package com.xqboss.web.controller.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqboss.apps.domain.sys.SysAppVersion;
import com.xqboss.apps.dto.sys.SysAppVersionDto;
import com.xqboss.apps.enums.sys.SaleableEnum;
import com.xqboss.apps.service.sys.SysAppVersionService;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import com.xqboss.common.utils.LocalDateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * app版本管理接口
 *
 * @author wangcong
 * @date 2023-09-04
 */
@Api(tags = "app版本管理")
@RestController
@RequestMapping("/web/app/version")
public class WebSysAppVersionController extends BaseController {
    @Autowired
    private SysAppVersionService sysAppVersionService;

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:app:version:add')")
    @ApiOperation("新增app版本")
    @PostMapping("/add")
    @Log(title = "App版本", businessType = BusinessType.INSERT)
    public R add(@RequestBody SysAppVersion dto) {
        dto.setOptUserId(getUserId());
        dto.setOptName(getUsername());
        boolean b = sysAppVersionService.save(dto);
        if (!b) {
            return R.fail("新增失败");
        }
        return R.ok();
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:app:version:update')")
    @ApiOperation("修改app版本")
    @PutMapping("/update")
    @Log(title = "App版本", businessType = BusinessType.UPDATE)
    public R update(@RequestBody SysAppVersion dto) {
        SysAppVersion version = sysAppVersionService.getById(dto.getId());
        if (ObjUtil.isNull(version)) {
            return R.fail("资源不存在");
        }
        SysAppVersion update = BeanUtil.copyProperties(dto, SysAppVersion.class);
        if (dto.getSaleable() == SaleableEnum.SEQUENCE) {
            update.setPublishTime(LocalDateUtils.getCurrentLocalDateTime());
        }
        update.setUpdateTime(LocalDateUtils.getCurrentLocalDateTime());
        sysAppVersionService.updateById(update);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:app:version:info')")
    @ApiOperation("版本详情")
    @GetMapping("/info/{id}")
    public R<SysAppVersion> findByKey(@PathVariable("id") Long id) {
        return R.ok(sysAppVersionService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('web:app:version:list')")
    @ApiOperation("条件查询")
    @GetMapping("/list")
    public R<List<SysAppVersion>> findBy(SysAppVersionDto dto) {
        dto.startPage();
        LambdaQueryWrapper<SysAppVersion> wq = new LambdaQueryWrapper<>();
        if (ObjUtil.isNotNull(dto)) {
            if (ObjUtil.isNotNull(dto.getSaleable())) {
                wq.eq(SysAppVersion::getSaleable, dto.getSaleable());
            }
            if (StrUtil.isNotEmpty(dto.getVersionName())) {
                wq.like(SysAppVersion::getVersionName, dto.getVersionName());
            }
            if (StrUtil.isNotEmpty(dto.getAppCode())) {
                wq.eq(SysAppVersion::getAppCode, dto.getAppCode());
            }
        }
        wq.orderByDesc(SysAppVersion::getCreateTime);
        List<SysAppVersion> list = sysAppVersionService.list(wq);
        return R.ok(list);
    }
}
